package cn.scu.ne04jdemo.controller;

import cn.scu.ne04jdemo.entitiy.ScheDevInfoEntity;
import cn.scu.ne04jdemo.entitiy.ScheDevTypeInfoEntity;
import cn.scu.ne04jdemo.service.ScheDevTypeInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sche/dev/type/info")
public class ScheDevTypeInfoController {

    @Autowired
    private ScheDevTypeInfoService scheDevTypeInfoService;

    @GetMapping("/findPage")
    public ResponseEntity<IPage> findPage(@RequestParam(required = false) String devTypeName,
                                          @RequestParam(defaultValue = "1") int pageNum,
                                          @RequestParam(defaultValue = "10") int pageSize) {
        IPage<ScheDevTypeInfoEntity> page = null;
        try {
            page = scheDevTypeInfoService.findPage(new Page<>(pageNum, pageSize), devTypeName);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.ok(page);
    }

    @GetMapping("/getList")
    public ResponseEntity<List> findPage() {
        List<ScheDevTypeInfoEntity> list = scheDevTypeInfoService.list();
        return ResponseEntity.ok(list);
    }


    @PostMapping("/delById")
    public Boolean delById(@RequestBody ScheDevTypeInfoEntity scheDevTypeInfoEntity) {
        try {
            if(ObjectUtils.isNotNull(scheDevTypeInfoEntity)){
                String id = scheDevTypeInfoEntity.getId();
                scheDevTypeInfoService.removeById(id);
            }else{
                throw new Exception();
            }

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @PostMapping("/insert")
    @ResponseBody
    public Boolean insert(@RequestBody ScheDevTypeInfoEntity scheDevTypeInfoEntity){
        try {
            if(!ObjectUtils.isNotNull(scheDevTypeInfoEntity))
                scheDevTypeInfoEntity = new ScheDevTypeInfoEntity();
            scheDevTypeInfoService.save(scheDevTypeInfoEntity);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
